package tiMu_9;
import com.sun.source.tree.YieldTree;

import javax.lang.model.element.Name;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Bank implements  Runnable{
      ArrayList<User> userlist=new ArrayList<>();

     Bank(ArrayList<User> a){
         userlist=a;
     }
   public void cunqian(User u,double num){
       if(num>0)
       {
           u.setMoney(num+u.getMoney());
           u.setCunTime(new Date());
           DateFormat format=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
           System.out.println(Thread.currentThread().getName()+u.getName()+"在"+format.format(u.getCunTime())+"存钱"+num+"元，余额为："+u.getMoney());
       }
   }

    public void quqian(User u,double num){
        double flag=u.getMoney()-num;
        if(flag>0)
        {
            u.setMoney(u.getMoney()-num);
            u.setCunTime(new Date());
            DateFormat format=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            System.out.println(Thread.currentThread().getName()+":"+u.getName()+"在"+format.format(u.getCunTime())+"取钱"+num+"元，余额为："+u.getMoney());
        }

    }

     public User getNameById(String carNumber,String pass){
         for (User a:userlist){
             if(a.carNumber.equals(carNumber)&&a.password.equals(pass)){//如果登录名和密码都相同的话  ，就是这个用户
                 return a;
             }
         }
         return null;
     }

    @Override
    public void run() {
        /* Scanner sc=new Scanner(System.in);
        System.out.println("--------欢迎"+Thread.currentThread().getName()+"来到网上银行--------");
        System.out.println("请输入您的银行卡卡号：");
        String carId=sc.nextLine();
        System.out.println("请输入您的银行卡密码：");
        String pass=sc.nextLine();
        User use=getNameById(carId,pass);//得到用户
        if(use==null)
        {
            System.out.println("您所输入的用户名或密码错误");
            Thread.currentThread().stop();
        }
        else {
            System.out.println(use.name+",您好!");
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
            User use = null;
              if(Thread.currentThread().getName()=="用户1")
              {
                  use=getNameById(userlist.get(0).carNumber,userlist.get(0).password);//得到用户
              }
              else if(Thread.currentThread().getName()=="用户2")
              {
                  use=getNameById(userlist.get(1).carNumber,userlist.get(1).password);//得到用户
              }
        int flag=0;
        while (flag!=40)
        {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (this)
                {
                cunqian(use,200);
                quqian(use,100);
                flag++;
               }
               Thread.yield();
 /*         System.out.println("---------------------");
            System.out.println("1 存钱");
            System.out.println("2 取钱");
            System.out.println("3 查询余额");
            System.out.println("4 结束操作");
            System.out.println("请输入你的选择：");
            System.out.println("---------------------");
            int i = sc.nextInt();
            if (i == 1) {
                cunqian(use);
            } else if (i == 2) {
                quqian(use);
            } else if (i == 3) {
                use.setCunTime(new Date());
                DateFormat format=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                System.out.println(Thread.currentThread().getName()+":"+use.getName()+"在"+format.format(use.getCunTime())+"查询余额为："+use.getMoney());
            }else  if(i==4){
                System.out.println("-"+use.getName()+"谢谢您的使用！-");
                flag=false;
                Thread.currentThread().stop();
            }*/
        }
    }
}
